<?php
namespace backend\models;
use yii\base\Model;
use common\models\DepotPosition;

class DepotPositionForm extends Model
{
    public  $sn;
    public $num;
    public $begin;
    public $depot_id;
    public  function rules()
    {
        return [
           [['num','begin','depot_id'],'required'],
            [['sn'],'safe'],
        ];
    }
    
    public function attributeLabels()
    {
        return [
           'num'=>'仓位个数',
            'begin'=>'起始编号',
            'sn'=>'编号前缀'
        ];
    }
    
    public function save()
    {
//        $model=new DepotPosition(); 
       $data=[];
       for($i=$this->begin,$j=0;$i<$this->begin+$this->num;$i++,$j++)
       {
           $position=DepotPosition::find()->where(['depot_id'=>$this->depot_id,'sn'=>$this->sn.$i])->one();
           if($position)
           {
               continue;
           }
           $data[$j]['depot_id']=$this->depot_id;
           $data[$j]['sn']=$this->sn.$i;
           $data[$j]['created_at']=time();
       }
       if($data)
       {
       return \Yii::$app->db->createCommand()->batchInsert(DepotPosition::tableName(),['depot_id','sn', 'created_at'], $data)->execute();//执行批量添加
       }else{
        return true;
       }
    }
}